<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>

<body>
  <h1>学习websocket</h1>
  <button id="btn">给服务器发数据</button>
  <script>
    const ws = new WebSocket("ws://127.0.0.1:3000");
    ws.onopen = () => {
      console.log("去连接服务器~");
      ws.send("我是客户端的数据~")
    }
    ws.onmessage = event => {
      console.log("收到了服务器的信息：", event.data);
    }

    let btn = document.getElementById("btn");
    btn.addEventListener("click", function () {
      ws.send("当前时间：" + Date.now())
    })
  </script>
</body>

</html>